package com.newer.weatherforecast.dao;


import java.util.ArrayList;
import java.util.List;

import com.newer.weatherforecast.entity.SmsInfo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;


public class SmsDAO extends BaseDAO{
	public SmsDAO(Context context) {
		super(context);
	}
	public void insertSmsInfo(SmsInfo sms){
		ContentValues values = new ContentValues();
		values.put("sms_num", sms.getSms_num());
		values.put("sms_body", sms.getSms_body());
		values.put("sms_name", sms.getSms_name());
		values.put("receive_time", sms.getReceive_time());
		values.put("return_result", sms.getReturn_result());
		long r = sqlDB.insert(SMS, null, values);
	}
	public List<SmsInfo> queryAllSms(){
		List<SmsInfo> list = new ArrayList<SmsInfo>();
		Cursor cursor = sqlDB.query(SMS, new String[]{"distinct sms_num,sms_name"}, null, null, null, null, null);
		int count = cursor.getCount();
		if(count==0||!cursor.moveToFirst()){
			cursor.close();
			return null;
		}
		for (int i = 0; i < count; i++) {
			SmsInfo smsInfo = new SmsInfo();
			smsInfo.setSms_num(cursor.getString(0));
			smsInfo.setSms_name(cursor.getString(1));
			list.add(smsInfo);
			cursor.moveToNext();
		}
		cursor.close();
		return list;
	}
	public List<SmsInfo> queryByNum(String num){
		List<SmsInfo> list = new ArrayList<SmsInfo>();
		Cursor cursor = sqlDB.query(SMS, null, "sms_num = " + num, null, null, null, null);
		int count = cursor.getCount();
		if(count==0||!cursor.moveToFirst()){
			cursor.close();
			return null;
		}
		for (int i = 0; i < count; i++) {
			SmsInfo smsInfo = new SmsInfo();
			smsInfo.setSms_num(cursor.getString(1));
			smsInfo.setSms_body(cursor.getString(2));
			smsInfo.setSms_name(cursor.getString(3));
			smsInfo.setReceive_time(cursor.getString(4));
			smsInfo.setReturn_result(cursor.getString(5));
			list.add(smsInfo);
			cursor.moveToNext();
		}
		cursor.close();
		return list;
	}
	public void deleteByNum(SmsInfo smsInfo){
		sqlDB.delete(SMS, "sms_num = " + smsInfo.getSms_num(), null);
	}
	public void clearAll(){
		sqlDB.delete(SMS, null, null);
	}
}
